﻿using System;
using System.Collections;
using System.Configuration;
using System.Data; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Utility;
using System.Text;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity; 

namespace Runda.CMS.Portal.NoticeManage
{
    public partial class DutyList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDuty(AspNetPager1.CurrentPageIndex);

                if (! base.CheckUserHasFuction(1, "Func1310"))
                {
                    spanAddDuty.Visible = false;
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindDuty(AspNetPager1.CurrentPageIndex);
        }

        protected void gridAccessoryList_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "delete")
            {
                SystemBll bll = new SystemBll();
                bll.Delete<SysDutyEntity>(" and DutyID='" + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["DutyID"].ToString() + "'");
            }
            BindDuty(AspNetPager1.CurrentPageIndex);
        }

        protected void gridAccessoryList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            { 
            
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindDuty(AspNetPager1.CurrentPageIndex);
        }

        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            BindDuty(AspNetPager1.CurrentPageIndex);

        } 
        private void BindDuty(int pageindex)
        {
            StringBuilder sb = new StringBuilder();

            if (txtDateFrom.SelectedDate.HasValue)
            {
                sb.Append(" and [Date] >= '").Append(txtDateFrom.SelectedDate.Value).Append("'");
            }

            if (txtDateTo.SelectedDate.HasValue)
            {
                sb.Append(" and [Date] <= '").Append(txtDateTo.SelectedDate.Value).Append("'");
            }

            int RecordCount = 0;
            DataTable dt = new SystemBll().QueryDutyAll(sb.ToString(), "Date asc", AspNetPager1.PageSize, pageindex, out RecordCount);

            AspNetPager1.RecordCount = RecordCount;
            gridAccessoryList.DataSource = dt;
            gridAccessoryList.DataBind();
        }
    }
}
